package com.wwj.code.login.token.controller;


import cn.hutool.extra.qrcode.QrCodeUtil;
import com.wwj.code.login.token.service.UserTokenService;
import com.wwj.code.util.numberUtils.SnowIdUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wwj
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/Token")
public class TokenController {


    @Resource
    private UserTokenService tokenService;


    @RequestMapping(value = "/get")
    public void get(HttpServletRequest request) {


        request.getSession().setAttribute("token1", "admin1");


    }


    @RequestMapping("/index")
    public String demo() {
        return "index";
    }

    //获取登录二维码、放入Token
    @RequestMapping(value = "/getLoginQr", method = RequestMethod.GET)
    public void createCodeImg(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");

        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        try {
            //这里没啥操作 就是生成一个UUID插入 数据库的表里
            String uuId = tokenService.createQrImg();
            response.setHeader("uuId", uuId);
            // 这里是开源工具类 hutool里的QrCodeUtil
            // 网址：http://hutool.mydoc.io/
            QrCodeUtil.generate(uuId, 300, 300, "jpg", response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
